import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author zhu
 * @date 2024-04-01
 */
public class People<P> {
    /**
     * 某个主人所养的宠物
     */
    private P pet;

    public People() {
    }

    public People(P pet) {
        this.pet = pet;
    }

    public P getPet() {
        return pet;
    }

    public void setPet(P pet) {
        this.pet = pet;
    }

    /**
     * 带宠物逛街
     */
    public void shopping() {
        // 1.获取宠物对象的 Class 对象
        Class<?> clazz = pet.getClass();

        // 2.获取宠物的姓名
        try {
            Field nameFiled = clazz.getDeclaredField("name");
            nameFiled.setAccessible(true);
            Object name = nameFiled.get(pet);
            System.out.println("某主人带着" + name + "逛街...");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            // 3.获取宠物卖萌叫的方法并调用方法 - 反射实现在运行中，动态调用方法..
            Method maiMengJiao = clazz.getDeclaredMethod("maiMengJiao");
            maiMengJiao.invoke(pet);
        } catch (Exception e) {
            throw new RuntimeException("此宠物不会卖萌叫"+e);
        }


    }
}
